
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">Ip</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="hidden" autocomplete="off" name="ip[]" value="like" class="layui-input inline-block">
                                <input type="text" autocomplete="off" placeholder="请输入Ip" name="ip[]" value="{$params['ip']}" class="layui-input">
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">URL</label>
                        <div class="layui-input-block">
                            <input type="text" name="uri" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">类型</label>
                        <div class="layui-input-block">
                            <div name="method" id="method" value="{$params['method']}" ></div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">响应结果</label>
                        <div class="layui-input-block">
                            <textarea name="response" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">created_at</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="created_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="admin.app.globallog20240420.insert">
                <i class="layui-icon layui-icon-add-1"></i>新增
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="admin.app.globallog20240420.delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="admin.app.globallog20240420.update">编辑</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="admin.app.globallog20240420.delete">删除</button>
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        <script src="/app/admin/admin/js/layuiAdmin.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/admin/app/global-log20240420/select";
            const UPDATE_API = "/admin/app/global-log20240420/update";
            const DELETE_API = "/admin/app/global-log20240420/delete";
            const INSERT_URL = "/admin/app/global-log20240420/insert";
            const UPDATE_URL = "/admin/app/global-log20240420/update";
            
            // 字段 类型 method
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/dict/get/upload",
                    dataType: "json",
                    success: function (res) {
                        let value = layui.$("#method").attr("value");
                        let initValue = value ? value.split(",") : [];
                        layui.xmSelect.render({
                            el: "#method",
                            name: "method",
                            initValue: initValue,
                            filterable: true,
                            data: res.data, 
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        });
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });
            
            // 字段 created_at created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                });
            })
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "id",align: "center",
						field: "id",
						hide: true,
					},{
						title: "Ip",align: "center",
						field: "ip",
					},{
						title: "URL",align: "center",
						field: "uri",
					},{
						title: "类型",align: "center",
						field: "method",
						templet: function (d) {
						    return layuiTemplet(d,"method",apiResults)
						}
					},{
						title: "应用平台appid",align: "center",
						field: "appid",
						templet: function (d) {
						    return layuiTemplet(d,"appid",apiResults)
						}
					},{
						title: "traceid",align: "center",
						field: "traceid",
						templet: function (d) {
						    return layuiTemplet(d,"traceid",apiResults)
						}
					},{
						title: "来源页",align: "center",
						field: "referer",
						hide: true,
					},{
						title: "user_agent",align: "center",
						field: "user_agent",
						hide: true,
					},{
						title: "请求参数",align: "center",
						field: "query",
						hide: true,
					},{
						title: "响应错误码",align: "center",
						field: "errcode",
						templet: function (d) {
						    return func.templetSwitch(d, "errcode");
						}
					},{
						title: "响应结果",align: "center",
						field: "response",
						hide: true,
					},{
						title: "异常信息",align: "center",
						field: "exception",
					},{
						title: "执行时间，单位毫秒",align: "center",
						field: "exec_time",
						sort: true,
					},{
						title: "请求cookie",align: "center",
						field: "cookie",
						hide: true,
					},{
						title: "created_at",align: "center",
						field: "created_at",
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 120,
					}
				];
				
				// 获取初始表现
				cols = layuiTableFilter(SELECT_API,cols)
				
				// 渲染表格
				function render()
				{
				    layuiRender({
				        elem: "#data-table",
				        url: SELECT_API,
				        cols:[cols],
                        exportAction:layuiExportAction,

				    });
				}
				
				// 获取表格中下拉或树形组件数据
				let apis = [];
				apis.push(["method", "/app/admin/dict/get/upload"]);
				apis.push(["appid", "/admin/utils/format/index?format=admins"]);
				apis.push(["traceid", "/admin/utils/format/index?format=users_nickname"]);
				let apiResults = {};
				apiResults["method"] = [];
				apiResults["appid"] = [];
				apiResults["traceid"] = [];
				let count = apis.length;
				layui.each(apis, function (k, item) {
				    let [field, url] = item;
				    $.ajax({
				        url: url,
				        dateType: "json",
				        success: function (res) {
				            if (res.code) {
				                return layui.popup.failure(res.msg);
				            }
				            function travel(items) {
				                for (let k in items) {
				                    let item = items[k];
				                    apiResults[field][item.value] = item.name;
				                    if (item.children) {
				                        travel(item.children);
				                    }
				                }
				            }
				            travel(res.data);
				        },
				        complete: function () {
				            if (--count === 0) {
				                render();
				            }
				        }
				    });
				});
				if (!count) {
				    render();
				}
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        func.remove(obj);
                    } else if (obj.event === "edit") {
                        func.edit(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        func.add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        func.batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                layuiFormQuery('table-query');

                // 表格顶部搜索重置事件
                layuiFormReset('table-reset');

                // 表格排序事件
                layuiTableSort();

                // 新增 编辑 删除的方法集合
                 let func = lauiInit(PRIMARY_KEY,INSERT_URL,UPDATE_API,DELETE_API);
            })

        </script>
    </body>
</html>
